package com.whq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whq.utils.AccessTokenUtils;
import com.whq.utils.HttpClientUtils;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Map;

/**
 * Created by qpx on 2017/12/21.
 */
@RestController
public class MessageController {

    public static void main(String[] args) {
        int n = 27;
        while (true) {
            int q = n%2;

            if (q == 0) {
                n = n / 2;
                System.out.println(n);

                if (n==1) {
                    return;
                }
            } else {
                n = n * 3 + 1;
                System.out.println(n);

                if (n==1){
                    return;
                }
            }
        }
//        System.out.println(q);
    }

    @PostMapping("/ex/send/message")
    public JSONObject sendWxMessage(String toUser,String message) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
        Map<String,Object> objectMap = Maps.newHashMap();
        objectMap.put("touser",toUser);
        objectMap.put("template_id","CW96EvmLqI6e-30bel7YN5wXApbRdhQrLHc2sLRhobg");
        //objectMap.put("url","http://q308e79147.qicp.vip/showDtl");
        objectMap.put("url","https://www.baidu.com");
        Map<String,Object> data = Maps.newHashMap();
        objectMap.put("data",data);
        Map<String,Object> remark = Maps.newHashMap();
        remark.put("value",message);
        data.put("qpx",remark);
        System.out.println(JSON.toJSONString(objectMap));
        String jsonString = JSON.toJSONString(objectMap);
        //Map map = JSON.parseObject(jsonString, Map.class);
        String toKen = AccessTokenUtils.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+toKen;
        JSONObject post = HttpClientUtils.sendSSL(url, "POST", JSON.toJSONString(objectMap));
        return post;
    }

//    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
//        Map<String,Object> objectMap = Maps.newHashMap();
//        objectMap.put("touser","oVOI-53iYMFWNAhLFgmJeL9nQdxg");
//        objectMap.put("template_id","rTl8rRWfowTbSMHNVgDepkStRlPqKXLWtHbnm0e6Hcs");
//        objectMap.put("url","https://www.baidu.com");
//        Map<String,Object> data = Maps.newHashMap();
//        objectMap.put("DATA",data);
//        Map<String,Object> remark = Maps.newHashMap();
//        remark.put("value","请发红包1111111111");
//        data.put("qpx",remark);
//        System.out.println(JSON.toJSONString(objectMap));
//        String jsonString = JSON.toJSONString(objectMap);
//        Map<String,Object> map = JSON.parseObject(jsonString, Map.class);
//        String toKen = AccessTokenUtils.getAccessToken();
//        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+toKen;
//        JSONObject post = HttpClientUtils.sendSSL(url, "POST", JSON.toJSONString(objectMap));
//        System.out.println(JSON.toJSONString(post));
//    }
}
